cmake_minimum_required(VERSION 3.5)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin/)
project(mediastream C CXX)

set(3RDPARTY_LIBS "")

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -fPIC -D_REENTRANT")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c14 -Wall -fPIC -D_REENTRANT")

set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O1")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-as-needed")

# ---[ Google-gflags
find_package(GFlags)
include_directories(${GFLAGS_INCLUDE_DIRS})
list(APPEND 3RDPARTY_LIBS ${GFLAGS_LIBRARIES})

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# ---[ glog
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindGlog.cmake)
include_directories(${GLOG_INCLUDE_DIRS})
list(APPEND 3RDPARTY_LIBS ${GLOG_LIBRARIES})

# 查找FFmpeg库
# ---[ FFmpeg
find_package(FFmpeg REQUIRED)
if (FFMPEG_FOUND)
  include_directories(${FFMPEG_INCLUDE_DIR})
  list(APPEND 3RDPARTY_LIBS ${FFMPEG_LIBRARIES})
  set(HAVE_FFMPEG true)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_FFMPEG")
  if (WITH_FFMPEG_AVDEVICE)
     add_definitions(-DHAVE_FFMPEG_AVDEVICE)
  endif ()
else ()
  message(FATAL_ERROR "ffmpeg not found!")
endif ()

# include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindFFmpeg.cmake)
find_package(FFmpeg REQUIRED)
if (FFMPEG_FOUND)
  include_directories(${FFMPEG_INCLUDE_DIR})
  list(APPEND 3RDPARTY_LIBS ${FFMPEG_LIBRARIES})
  set(HAVE_FFMPEG true)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_FFMPEG")
  if (WITH_FFMPEG_AVDEVICE)
     add_definitions(-DHAVE_FFMPEG_AVDEVICE)
  endif ()
else ()
  message(FATAL_ERROR "ffmpeg not found!")
endif ()

# ---[ opencv
set(OpenCV_FIND_QUIETLY true)
#find_package(OpenCV REQUIRED) //this will include opencv_ts which result in crash on centos
find_package(OpenCV OPTIONAL_COMPONENTS videoio)
set(optional_opencv_libs ${OpenCV_LIBS})
find_package(OpenCV REQUIRED core imgproc highgui features2d)
if(OpenCV_FOUND)
  if(imgcodecs_FOUND)
    list(APPEND OpenCV_LIBS optional_opencv_libs)
  endif()
  include_directories(${OpenCV_INCLUDE_DIRS})
  list(APPEND 3RDPARTY_LIBS ${OpenCV_LIBS})
  message(STATUS "opencv include: ${OpenCV_INCLUDE_DIRS}")
  message(STATUS "opencv libraries: ${OpenCV_LIBS}")
else()
  message(FATAL_ERROR "opencv not found!")
endif()

include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  # ${CMAKE_CURRENT_SOURCE_DIR}/decode
  # ${CMAKE_CURRENT_SOURCE_DIR}/encode
  # ${CMAKE_CURRENT_SOURCE_DIR}/inference
  # ${CMAKE_CURRENT_SOURCE_DIR}/osd
  ${CMAKE_CURRENT_SOURCE_DIR}/common
  ${CMAKE_CURRENT_SOURCE_DIR}/framework
)

file(GLOB_RECURSE module_srcs
  # ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
  # ${CMAKE_CURRENT_SOURCE_DIR}/encode/*.cpp
  # ${CMAKE_CURRENT_SOURCE_DIR}/decode/*.cpp
  # ${CMAKE_CURRENT_SOURCE_DIR}/inference/*.cpp
  # ${CMAKE_CURRENT_SOURCE_DIR}/osd/*.cpp
  ${CMAKE_CURRENT_SOURCE_DIR}/framework/*.cpp
)
file(GLOB stream_src ${CMAKE_CURRENT_SOURCE_DIR}/stream_app.cpp)
list(APPEND srcs ${module_srcs} ${stream_src})

add_executable(stream_app ${srcs})
target_link_libraries(stream_app PUBLIC ${3RDPARTY_LIBS} glog -dl -pthread -lm)

if (BUILD_TESTS)
  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/googletest)
endif()
